package com.twu.biblioteca;

import java.util.Scanner;

public class Biblioteca {
    private Library library = new Library();
    private Menu menu = new Menu();

    public static void main(String[] args) {
        Biblioteca biblioteca = new Biblioteca();
        biblioteca.initialize();
        biblioteca.welcome();
        biblioteca.run();
    }

    public void initialize() {
        Book bookJava = new Book(1, "Core Java", "Cay S. Horstmann", "SunSoft Press");
        Book bookRuby = new Book(2, "Everyday scripting with ruby", "Brian Marick", "Pragmatic Bookshelf");
        library.addToBookList(bookJava);
        library.addToBookList(bookRuby);

        Movie movie1 = new Movie("Sholay", "1975", "Ramesh Sippy");
        Movie movie2 = new Movie("Gandhi", "1982", "Richard Samuel Attenborough", 2);
        library.addToMovieList(movie1);
        library.addToMovieList(movie2);

        User user1 = new User("123", "zhangsan", "zhangsan@gmail.com", "110");
        User user2 = new User("321", "lisi", "lisi@gmail.com", "120");
        library.addToUserList(user1);
        library.addToUserList(user2);

        ShowAllBooksAction showAllBooksAction = new ShowAllBooksAction(library);
        ReserveBookAction reserveBookAction = new ReserveBookAction(library);
        CheckMembershipAction checkMembershipAction = new CheckMembershipAction(library);
        ShowAllMoviesAction showAllMoviesAction = new ShowAllMoviesAction(library);
        LoginAction loginAction = new LoginAction(library);

        menu.setAction(1, showAllBooksAction);
        menu.setAction(2, reserveBookAction);
        menu.setAction(3, checkMembershipAction);
        menu.setAction(4, showAllMoviesAction);
        menu.setAction(5, loginAction);
    }

    private void welcome() {
        System.out.println("Welcome to Biblioteca!");
    }

    public void outputOptions() {
        System.out.println("\n1. Show all books");
        System.out.println("2. Reserve a book");
        System.out.println("3. Check membership details");
        System.out.println("4. Show all movies");
        System.out.println("5. Login");
        System.out.print("Please choose one option(Input 0 to exit):");
    }

    public void run() {
        int choice = 0;

        outputOptions();
        Scanner scanner = new Scanner(System.in);
        if (scanner.hasNextInt()) {
            choice = scanner.nextInt();
        }

        do {
            if (choice != 2 || library.isUserLoggedIn()) {
                String result = menu.optionChosen(choice);
                System.out.println(result);
            } else {
                System.out.println("You need to login for this operation.");
            }
            System.out.print("\nPlease choose one option(Input 0 to exit):");

            if (scanner.hasNextInt()) {
                choice = scanner.nextInt();
            }
        } while (choice != 0);
    }
}
